
//
//  BuyCountView.m
//  AddShoppingCart
//
//  Created by 主用户 on 16/3/23.
//  Copyright © 2016年 江萧. All rights reserved.
//

#import "BuyCountView.h"

@implementation BuyCountView
@synthesize bt_add,bt_reduce,tf_count,lb,count;
-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIView*v=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, 1)];
        v.backgroundColor=Gray_Color;
        [self addSubview:v];
        [self creatUI];
    }
    return self;
}
-(void)creatUI{
    lb = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    lb.text = @"购买数量";
    lb.textColor = TextColor3;
    lb.font = FontAndStyle14;
    [self addSubview:lb];
    
    bt_add= [UIButton buttonWithType:UIButtonTypeCustom];
    bt_add.frame = CGRectMake(self.frame.size.width-10-40, 10,40, 30);
    [bt_add setBackgroundColor:[UIColor whiteColor]];
    
    [bt_add setTitleColor:TextColor3 forState:0];
    bt_add.titleLabel.font = [UIFont systemFontOfSize:20];
    [bt_add setTitle:@"+" forState:0];
    [self addSubview:bt_add];
    tf_count = [[UITextField alloc] initWithFrame:CGRectMake(bt_add.frame.origin.x -39, 10, 40, 30)];
    tf_count.text = @"1";
    tf_count.delegate=self;
    tf_count.textAlignment = NSTextAlignmentCenter;
    tf_count.font = [UIFont systemFontOfSize:15];
    tf_count.backgroundColor = [UIColor whiteColor];
    tf_count.textColor=TextColor3;
    tf_count.keyboardType=UIKeyboardTypeNumberPad;
    //[self addSubview:tf_count];
    count = [[UITextField alloc] initWithFrame:CGRectMake(bt_add.frame.origin.x -39, 10, 40, 30)];
    count.text = @"1";
    count.delegate=self;
    count.textAlignment = NSTextAlignmentCenter;
    count.font = [UIFont systemFontOfSize:15];
    count.backgroundColor = [UIColor whiteColor];
    count.textColor=TextColor3;
    count.keyboardType=UIKeyboardTypeNumberPad;
    [self addSubview:count];
    
    bt_reduce= [UIButton buttonWithType:UIButtonTypeCustom];
    bt_reduce.frame = CGRectMake(tf_count.frame.origin.x -39, 10, 40, 30);
    [bt_reduce setBackgroundColor:[UIColor whiteColor]];
    [bt_reduce setTitleColor:TextColor3 forState:0];
    bt_reduce.titleLabel.font = [UIFont systemFontOfSize:20];
    [bt_reduce setTitle:@"-" forState:0];
    [self addSubview:bt_reduce];
    bt_add.layer.borderColor=SeparatorColor.CGColor;
    bt_add.layer.borderWidth=1;
    bt_reduce.layer.borderColor=SeparatorColor.CGColor;
    bt_reduce.layer.borderWidth=1;
    count.layer.borderColor=SeparatorColor.CGColor;
    count.layer.borderWidth=1;
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height-0.5, self.frame.size.width, 0.5)];
    line.backgroundColor = Gray_Color;
    [self addSubview:line];
}
-(UIToolbar *)addToolbar
{
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, MainW, 50)];
    //    UIToolbar *toolbar =[[UIToolbar alloc] init];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
    toolbar.items = @[space,bar];
    toolbar.tintColor=Red_Color;
    return toolbar;
}
-(void)textFieldDone{
    [self endEditing:YES];
    [_delegate changeValue];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [_delegate textFieldDidBeginEditing];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField==count){
        count.inputAccessoryView=[self addToolbar];
    }
    return YES;
}

@end
